package api.person;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Ann Raboschuk
 */
public class Person implements IPerson
{
   private static final long serialVersionUID = 1L;
   private Long id;
   private String surname, name;
   private Integer age;
   private Character sex;
   private String email;
   private Set<Phone> phone;
   private Set<Address> address;   

   public Person()
   {
      phone = new HashSet<IPhone>();
   }
   
   public Person(Long id, String surname, String name, int age, char sex, String email,
         Set<IPhone> telephone, Set<Address> address)
   {
      this.id = id;
      this.surname = surname;
      this.name = name;
      this.age = age;
      this.sex = sex;
      this.email = email;
      this.phone = telephone;
      this.address = address;
   }
   
   @Override
   public String toString()
   {
      StringBuilder phones = new StringBuilder();
      for (Phone telephone : phone)
         phones.append(telephone + " + ");
      StringBuilder addresses = new StringBuilder();
      for (Address addr : address)
         addresses.append(addr + " + ");
      return "" + id + "; " + surname + "; " + name + "; " + age + "; " + sex
            + "; " + email + "; "
            + phones.toString().substring(0, phones.length() - 3) + "; "
            + addresses.toString().substring(0, addresses.length() - 3);
   }
   
   @Override
   public int compareTo(IPerson object)
   {
      Long id2 = new Long(object.getId());
      return new Long(id).compareTo(id2);
   }
   
   public static Person fromString(String object)
   {
      String[] fields = object.split("; ");
      String[] phones = fields[6].split(" + ");
      Set<String> phonesSet = new HashSet<String>();
      for (int i=0; i<phones.length; i++)
         phonesSet.add(phones[i]);
      
      String[] addresses = fields[7].split(" + ");
      Set<String> addressesSet = new HashSet<String>();
      for (int i=0; i<addresses.length; i++)
         addressesSet.add(addresses[i]);
      
      return new Person(Long.parseLong(fields[0]), fields[1], fields[2],
            Integer.parseInt(fields[3]), fields[4].charAt(0), fields[5],
            phonesSet, addressesSet);
   } 
   
   public boolean equals(Object object)
   {
      boolean equal = false;
      if (object != null && object instanceof Person)
      {
         Person person = (Person) object;
         equal = (id == person.getID()) && surname.equals(person.getSurname())
               && name.equals(person.getName()) && (age == person.getAge())
               && (sex == person.getSex()) && email.equals(person.getEmail())
               && phone.equals(person.getPhone())
               && address.equals(person.getAddress());
      }
      return equal;
   }
   
   public Long getId()
   {
      return id;
   }
   
   public String getSurname()
   {
      return surname;
   }
   
   public String getName()
   {
      return name;
   }
   
   public Integer getAge()
   {
      return age;
   }
   
   public Character getSex()
   {
      return sex;
   }
   
   public String getEmail()
   {
      return email;
   }
   
   public Set<Phone> getPhone()
   {
      return phone;
   }
   
   public Set<Address> getAddress()
   {
      return address;
   }
   
   public void setId(Long id)
   {
      this.id = id;
   }
   
   public void setSurname(String surname)
   {
      this.surname = surname;
   }
   
   public void setName(String name)
   {
      this.name = name;
   }
   
   public void setAge(Integer age)
   {
      this.age = age;
   }
   
   public void setSex(Character sex)
   {
      this.sex = sex;
   }
   
   public void setEmail(String email)
   {
      this.email = email;
   }
   
   public void setPhone(Set<IPhone> phone)
   {
      this.phone = phone;
   }
   
   public void setAddress(Set<Address> address)
   {
      this.address = address;
   }
}
